//
//  UITextField+RGAppUtitils.m
//  MovingLoaniOS
//
//  Created by RAIN on 2019/2/18.
//  Copyright © 2019 GWMFC. All rights reserved.
//

#import "UITextField+RGAppUtitils.h"
#import <objc/runtime.h>



#define kDNUMBERS   @"0123456789\n"
#define kDLETTER    @"abcdefghijklmnopqresuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

static NSString * const GMaxLengthKey = @"GMaxLengthKey";
static NSString * const GTextLengthBlockKey = @"GTextLengthKey";

static NSString * const GTextFieldTypeKey = @"GTextFieldTypeKey";
static NSString * const GIsTextFieldTypeKey = @"GIsTextFieldTypeKey";

@implementation UITextField (RGAppUtitils)

- (void)setMaxLength:(NSInteger)maxLength {
    objc_setAssociatedObject(self, &GMaxLengthKey, @(maxLength), OBJC_ASSOCIATION_ASSIGN);
    
    [self addTarget:self
             action:@selector(textFieldDidChange:)
   forControlEvents:UIControlEventEditingChanged];
}

- (NSInteger)maxLength {
    return [objc_getAssociatedObject(self, &GMaxLengthKey) integerValue];
}

- (void)setLengthBlock:(TextLengthBlock)lengthBlock {
    objc_setAssociatedObject(self, &GTextLengthBlockKey, lengthBlock, OBJC_ASSOCIATION_COPY);
    
    [self addTarget:self
             action:@selector(textFieldDidChange:)
   forControlEvents:UIControlEventEditingChanged];
}

- (TextLengthBlock)lengthBlock {
    return objc_getAssociatedObject(self, &GTextLengthBlockKey);
}

- (void)lengthLimitBlock:(UITextField *)textField {
    if (self.lengthBlock) {
        self.lengthBlock(textField.text.length);
    }
}

- (void)textFieldDidChange:(UITextField *)textField {
    
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    
    if (!position){
        
        if (textField.text.length > textField.maxLength){
            
            NSRange rangeIndex = [textField.text rangeOfComposedCharacterSequenceAtIndex:textField.maxLength];
            if (rangeIndex.length == 1){
                
                textField.text = [textField.text substringToIndex:textField.maxLength];
            }
            else{
                
                NSRange rangeRange = [textField.text rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, textField.maxLength)];
                textField.text = [textField.text substringWithRange:rangeRange];
            }
        }
    }
   // textField.text = [textField.text substringWithRange:[self getTextFieldRange:textField]];
    /*
    switch (self.textFieldType) {
        case TextFieldStringTypeNone: {
            [self textFieldStringTypeNumber:textField];
        }
            break;

        case TextFieldStringTypeNumber: {
            [self textFieldStringTypeNumber:textField];
        }
            break;

        case TextFieldStringTypeLetter: {
            [self textFieldStringTypeLetter:textField];
        }
            break;

        default:
            break;
    }
    */

    [self lengthLimitBlock:textField];
}

/**
 无限制（可以输入任何类型）
 @param textField textField
 */
- (void)textFieldStringTypeNone:(UITextField *)textField {
    textField.text = [textField.text substringWithRange:[self getTextFieldRange:textField]];
}


/**
 获取TextField输入范围
 @param textField textField
 @return 范围
 */
- (NSRange)getTextFieldRange:(UITextField *)textField {
    NSInteger adaptedLength = textField.maxLength > 0 ? MIN(textField.text.length, textField.maxLength) : MIN(textField.text.length, MAXFLOAT);
    NSRange range = NSMakeRange(0, adaptedLength);
    
    return range;
}



- (void)rg_addRightViewWithImageName:(NSString *)imageName {
    CGRect rightViewFrame = CGRectMake(0, 0, 18, 18);
    [self rg_addRightImageByName:imageName
                           frame:rightViewFrame
                     contentMode:UIViewContentModeCenter
                        showMode:UITextFieldViewModeAlways];
}

- (void)rg_addRightImageByName:(NSString *)name
                         frame:(CGRect)frame
                   contentMode:(UIViewContentMode)contentMode
                      showMode:(UITextFieldViewMode)showMode
{
    UIImageView *imageView = [self createImageViewByName:name
                                                   frame:frame
                                             contentMode:contentMode];
    self.rightViewMode = showMode;
    self.rightView = imageView;
}

#pragma mark - Private
- (UIImageView *)createImageViewByName:(NSString *)name
                                 frame:(CGRect)frame
                           contentMode:(UIViewContentMode)contentMode
{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:name];
    imageView.frame = frame;
    imageView.contentMode = contentMode;
    return imageView;
}

@end
